@file:Suppress("DEPRECATION")

package com.wanban.downloader.service

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import com.wanban.downloader.MultiDownloadManager
import com.wanban.downloader.R
import com.wanban.downloader.db.DownloadEntity
import com.wanban.downloader.manager.BaseDownloadManager


class DownloadService: Service() {

    private lateinit var downloadManager: BaseDownloadManager

    override fun onCreate() {
        super.onCreate()
        Log.e(BaseDownloadManager.TAG, "onCreate...下载器初始化")
        downloadManager = MultiDownloadManager.getDownloadManager()
        downloadManager.init(application)
        callStartForeground()
    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        val action = intent.getIntExtra("action", -1)
        if (action == -1) return super.onStartCommand(intent, flags, startId)
        if (action == 0) {
            downloadManager.resumeAllDownload()
            Log.e(BaseDownloadManager.TAG, "恢复所有下载....")
            return super.onStartCommand(intent, flags, startId)
        }
        Log.e(BaseDownloadManager.TAG, "下载服务启动....$action")
        val task: DownloadEntity = intent.getSerializableExtra("task") as DownloadEntity? ?: return super.onStartCommand(intent, flags, startId)
        when (action) {
            1 -> downloadManager.startDownload(task)
            2 -> downloadManager.stopDownload(task.url)
            3 -> downloadManager.resumeDownload(task.url)
            4 -> downloadManager.deleteDownload(task.url)
            else -> {}
        }
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e(BaseDownloadManager.TAG, "下载服务关闭....")
    }

    private fun callStartForeground() {
        val id = "com.wanban.downloader" //这里的id里面输入自己的项目的包的路径
        val intent = Intent()
        intent.setClass(this, NotificationReceiver::class.java)
        val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
        } else {
            PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        }
        val notification: NotificationCompat.Builder //创建服务对象
        val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(id, "Download", NotificationManager.IMPORTANCE_HIGH)
            channel.enableLights(true)
            channel.setShowBadge(true)
            channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            manager.createNotificationChannel(channel)
            notification = NotificationCompat.Builder(this).setChannelId(id)
        } else {
            notification = NotificationCompat.Builder(this)
        }
        notification.setContentTitle("下载")
            .setContentText("有新下载内容！")
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_download)
            .setContentIntent(pendingIntent)
            .build()
        val notification1 = notification.build()
        startForeground(1, notification1) //这个就是之前说的startForeground
    }

}